package org.ala.shopping_test.shopping_center.service;

import org.ala.shopping_test.ShardInfos;
import org.ala.shopping_test.shopping_center.repository.category.dao.ShoppingCategoryMapper;
import org.ala.shopping_test.shopping_center.repository.category.entity.ShoppingCategoryDO;
import org.ala.shopping_test.shopping_center.repository.category.entity.ShoppingCategoryFields;
import org.ala.shopping_test.shopping_center.repository.order.dao.ShoppingOrderMapper;
import org.ala.shopping_test.shopping_center.repository.order_buyer.dao.ShoppingOrderBuyerMapper;
import org.ala.shopping_test.shopping_center.repository.order_seller.dao.ShoppingOrderSellerMapper;
import org.ala.tiktools.sharddb.db.ThreadLocalLookupKey;
import org.ala.tiktools.sharddb.tools.SnowIdGenerator;
import org.ala.tiktools.tools.TimestampTools;
import org.ala.tiktools.tools.kv.KV;
import org.ala.tiktools.tools.kv.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ala
 * @date 2025-03-17 12:16
 */
@Component
public class ShoppingCategoryService {


    @Autowired
    protected ShoppingCategoryMapper shoppingCategoryMapper;
    protected SnowIdGenerator categoryIdGenerator = SnowIdGenerator.build(1);

    @Autowired
    protected ShoppingOrderMapper shoppingOrderMapper;
    protected SnowIdGenerator orderIdGenerator = SnowIdGenerator.build(1);

    @Autowired
    protected ShoppingOrderBuyerMapper shoppingOrderBuyerMapper;
    protected SnowIdGenerator orderBuyerIdGenerator = SnowIdGenerator.build(1);
    @Autowired
    protected ShoppingOrderSellerMapper shoppingOrderSellerMapper;
    protected SnowIdGenerator orderSellerIdGenerator = SnowIdGenerator.build(1);


    /**
     *  构建品类数据
     */
    public void createData() {
        ThreadLocalLookupKey.setLookup(ShardInfos.DB_SHOPPING_CENTER_MAIN);

        String[] roots = {"生活用品", "学习用品", "体育用品", "成人用品"};
        Map<String, Long> rootMapper = Arrays.stream(roots).collect(Collectors.toMap(e -> e, e -> categoryIdGenerator.id().getId(), (o,n) -> n));
        createDate(rootMapper);

        //  生活用品子类
        String[][] subs = {
                {"厨房", "洗漱", "床上", "服装"},
                {"文具", "书本", "书包"},
                {"球类", "田径", "户外"},
                {"神油", "蓝色小药丸", "TT", "战衣"}
        };
        List<ShoppingCategoryDO> list = new ArrayList<>();
        for (int i = 0; i < subs.length; i++) {
            for (int j = 0; j < subs[i].length; j++) {
                SnowIdGenerator.Id id = categoryIdGenerator.id();
                Timestamp now = TimestampTools.instanceOf(id.getTimestamp());
                list.add(ShoppingCategoryDO.build()
                        .id(id.getId())
                        .categoryName(subs[i][j])
                        .parentId(rootMapper.get(roots[i]))
                        .createTime(now)
                        .modifyTime(now)
                );
            }
        }
        shoppingCategoryMapper.multipleInsert(list);
        ThreadLocalLookupKey.clear();
    }
    protected void createDate(Map<String, Long> idMapper) {
        List<ShoppingCategoryDO> list = idMapper.entrySet().stream().map(e -> ShoppingCategoryDO.build().id(e.getValue()).categoryName(e.getKey()).createTime(TimestampTools.now()).modifyTime(TimestampTools.now())).collect(Collectors.toList());
        shoppingCategoryMapper.multipleInsert(list);
    }

}
